﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.Commons.DataFilters;

namespace SD.DataAccess
{
    // author: ToanTN, 21 Dec 2012
    public partial class Organization
    {
        private static ServiceDBDataContext Dc = new ServiceDBDataContext();

        /// <summary>
        /// Get list organizations
        /// </summary>
        /// <param name="filter">filter</param>
        /// <param name="startRowIndex">start row index</param>
        /// <param name="maxRows">max rows</param>
        /// <param name="sortType">sort type</param>
        /// <returns></returns>
        public static IQueryable<Organization> GetOrganizations(OrganizationFilter filter, int startRowIndex, int maxRows = 15, string sortType = null)
        {
            // filter data
            IQueryable<Organization> data = FilterOrganizations(filter);
            
            // sort data
            data = SortOrganizations(data, sortType);

            // get data
            data = data.Skip(startRowIndex).Take(maxRows);
            return data;
        }

        
        /// <summary>
        /// sort organization
        /// </summary>
        /// <param name="data">organization we want to sort</param>
        /// <param name="sortType">sort type</param>
        /// <returns></returns>
        private static IQueryable<Organization> SortOrganizations(IQueryable<Organization> data, string sortType)
        {
            bool sortDescending = false;
            if (!string.IsNullOrEmpty(sortType))
            {
                // the sortType is a string with format: "XXX YYY"
                // "XXX" is the column name that we want to sort
                // "YYY" is ASC or DESC. Default value is ASC

                string[] values = sortType.Split(' '); // split sort type into 2 parts
                sortType = values[0]; // get column name

                if (values.Length > 1)
                {
                    sortDescending = (values[1] == "DESC"); // Asc or Desc
                }

                switch (sortType)
                {
                    case "OrganisationName":
                        data = sortDescending
                                   ? data.OrderByDescending(o => o.OrganizationName)
                                   : data.OrderBy(o => o.OrganizationName);
                        break;

                    case "HeadOfficeAddressLine1":
                        data = sortDescending
                                   ? data.OrderByDescending(o => o.Address.AddressLine)
                                   : data.OrderBy(o => o.Address.AddressLine);
                        break;

                    case "PostCode":
                        data = sortDescending
                                   ? data.OrderByDescending(o => o.Address.Postcode)
                                   : data.OrderBy(o => o.Address.Postcode);
                        break;

                    case "Contact":
                        data = sortDescending
                                   ? data.OrderByDescending(o => o.Contact.FirstName).ThenBy(o => o.Contact.Surname)
                                   : data.OrderBy(o => o.Contact.FirstName).ThenBy(o => o.Contact.Surname);
                        break;

                    case "IsActive":
                        data = sortDescending ? data.OrderByDescending(o => o.IsActive) : data.OrderBy(o => o.IsActive);
                        break;
                }
            }
            return data;
        }

        /// <summary>
        /// Filter organizations
        /// </summary>
        /// <param name="filter">the filter</param>
        /// <returns></returns>
        private static IQueryable<Organization> FilterOrganizations(OrganizationFilter filter)
        {
            IQueryable<Organization> data = null;
            if (filter.InActive) // get all organizations
            {
                data = Dc.Organizations;
            }
            else
            {
                // just get active organization
                data = Dc.Organizations.Where(o => o.IsActive == true);
            }
            if (filter.Letters != null)
            {
                if (filter.Letters.Length > 1) // do not get all
                {                    
                    data = data.Where(o => filter.Letters.Contains(o.OrganizationName[0].ToString()))
                        .OrderBy(o => o.OrganizationName);
                }
            }
            return data;
        }

        /// <summary>
        /// Count organization
        /// </summary>
        /// <param name="filter">the filter</param>
        /// <returns></returns>
        public static int CountOrganizations(OrganizationFilter filter)
        {
            IQueryable<Organization> data = FilterOrganizations(filter);
            
            if (data.Any()) // has entity
            {
                return data.ToList().Count;
            }
            return 0;
        }

        /// <summary>
        /// Sets the active organization.
        /// </summary>
        /// <param name="organizationId">The organization id.</param>
        /// <param name="status">if set to <c>true</c> [status].</param>
        /// <returns></returns>
        public static bool SetActiveOrganization(int organizationId, bool status)
        {
            var organization = Dc.Organizations.FirstOrDefault(o => o.OrganizationId == organizationId);
            if (organization != null) // found
            {
                organization.IsActive = status; // change status
                Dc.SubmitChanges(); // save
                return true;
            }
            return false; // not found --> return false
        }

    }
}
